package com.yw.atp.model;

import java.util.Date;
import java.util.Set;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yw.atp.Constant;


@Entity
@Table(name=Constant.DB_PREFIX+"department")
@NamedQueries({
    @NamedQuery(name = "Department.getDepartments", 
        query = "select d FROM Department d where d.name like :dpartName and d.status !='1' order by d.id desc"
    )
})
public class Department implements IDEntity{
	private static final long serialVersionUID = 1L;

	@Id
    @GeneratedValue
    private Integer id;
	private String name;
	@OneToMany(mappedBy="department",fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
	private Set<Staff> staffs;
	@ManyToMany(mappedBy="deparList",fetch=FetchType.LAZY,cascade=CascadeType.PERSIST)
    private Set<Command> commandSet;
	
	@Temporal(TemporalType.TIMESTAMP)
	@OrderBy("createdAt desc")
	private Date createdAt;
    private String status = "0";
    private Date updateAt;
	public Date getUpdateAt() {
		return updateAt;
	}

	public void setUpdateAt(Date updateAt) {
		this.updateAt = updateAt;
	}
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	public Set<Command> getCommandSet() {
		return commandSet;
	}

	public void setCommandSet(Set<Command> commandSet) {
		this.commandSet = commandSet;
	}

	public Set<Staff> getStaffs() {
		return staffs;
	}

	public void setStaffs(Set<Staff> staffs) {
		this.staffs = staffs;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
    
	public static JSONArray capture(List<Department> appms) throws JSONException{
		JSONArray jsonAppms = new JSONArray();
		for(Department appm : appms){
			jsonAppms.put(appm.capture());
		}
		return jsonAppms;
	}
	
	public static JSONArray capture(Set<Department> appms) throws JSONException{
		JSONArray jsonAppms = new JSONArray();
		for(Department appm : appms){
			jsonAppms.put(appm.capture());
		}
		return jsonAppms;
	}
	
	public JSONObject capture() throws JSONException{
		JSONObject appm = new JSONObject();
		appm.put("id",id);
		appm.put("name",name);
		return appm;
	}
	
	public static JSONArray captureForSel(List<Department> appms) throws JSONException{
		JSONArray jsonAppms = new JSONArray();
		for(Department appm : appms){
			jsonAppms.put(appm.captureForSel());
		}
		return jsonAppms;
	}
	
	public JSONObject captureForSel() throws JSONException{
		JSONObject appm = new JSONObject();
		appm.put("id",id);
		appm.put("name",name);
		return appm;
	}
}
